﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Media.Media3D;
using System.IO;
using DataProcessing;
namespace Gene.Render
{
    public class GraphicUtil
    {
        static public System.Windows.Shapes.Path DrawLine(Point startPt, Point endPt, double thickness, Brush brush)
        {
            LineGeometry myLineGeometry = new LineGeometry();
            myLineGeometry.StartPoint = startPt;
            myLineGeometry.EndPoint = endPt;

            System.Windows.Shapes.Path myPath = new System.Windows.Shapes.Path();
            myPath.Stroke = brush;
            myPath.StrokeThickness = thickness;
            myPath.Data = myLineGeometry;

            return myPath;
        }
   
    
        public static TransformMatrix ToTransformMatrix(Matrix3D val)
        {
            TransformMatrix ret = new TransformMatrix();
            ret.M11 = val.M11;
            ret.M12 = val.M12;
            ret.M13 = val.M13;
            ret.M14 = val.M14;

            ret.M21 = val.M21;
            ret.M22 = val.M22;
            ret.M23 = val.M23;
            ret.M24 = val.M24;

            ret.M31 = val.M31;
            ret.M32 = val.M32;
            ret.M33 = val.M33;
            ret.M34 = val.M34;

            ret.OffsetX = val.OffsetX;
            ret.OffsetY = val.OffsetY;
            ret.OffsetZ = val.OffsetZ;
            ret.M44 = val.M44;
            return ret;
        }



        public static Matrix3D ToMatrix3D(TransformMatrix val)
        {
            Matrix3D ret = new Matrix3D();
            ret.M11 = val.M11;
            ret.M12 = val.M12;
            ret.M13 = val.M13;
            ret.M14 = val.M14;

            ret.M21 = val.M21;
            ret.M22 = val.M22;
            ret.M23 = val.M23;
            ret.M24 = val.M24;

            ret.M31 = val.M31;
            ret.M32 = val.M32;
            ret.M33 = val.M33;
            ret.M34 = val.M34;

            ret.OffsetX = val.OffsetX;
            ret.OffsetY = val.OffsetY;
            ret.OffsetZ = val.OffsetZ;
            ret.M44 = val.M44;
            return ret;
        }



        public static void SaveToImage(FrameworkElement p_FrameworkElement, string p_FileName)
        {
            System.IO.FileStream fs = new System.IO.FileStream(p_FileName, System.IO.FileMode.Create);
            RenderTargetBitmap bmp = new RenderTargetBitmap((int)p_FrameworkElement.ActualWidth, (int)p_FrameworkElement.ActualHeight, 96.0, 96.0, PixelFormats.Pbgra32);
            bmp.Render(p_FrameworkElement);
            bmp.Freeze();
            BitmapEncoder encoder = new BmpBitmapEncoder();

            encoder.Frames.Add(BitmapFrame.Create(bmp));
            //encoder.Save(fs);
            MemoryStream tmpStream = new MemoryStream();
            encoder.Save(tmpStream);


            tmpStream.Position = 0;

            //System.IO.FileStream fs2 = new System.IO.FileStream(@".\capture.png", System.IO.FileMode.Create);
            BitmapEncoder pngEncoder = new PngBitmapEncoder();
            pngEncoder.Frames.Add(BitmapFrame.Create(tmpStream));

            pngEncoder.Save(fs);


            fs.Flush();
            fs.Close();
            fs.Dispose();




        }

        public static BitmapSource GetImage(string url)
        {
            try
            {

                using (BinaryReader reader = new BinaryReader(File.Open(url, FileMode.Open)))
                {
                    FileInfo fi = new FileInfo(url);
                    byte[] bytes = reader.ReadBytes((int)fi.Length);
                    reader.Close();
                    BitmapImage bitmapImage = new BitmapImage();
                    bitmapImage.BeginInit();
                    bitmapImage.StreamSource = new MemoryStream(bytes);
                    bitmapImage.EndInit();
                    bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
                    return bitmapImage;
                }
            }
            catch (Exception ex)
            {
                string a = ex.ToString();
                return null;
            }
        }
       
    }
}
